package com.mcxx.modules.extremepoverty.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.extremepoverty.service.ExBacklogService;
import com.mcxx.modules.extremepoverty.service.ExQueryService;
import com.mcxx.modules.extremepoverty.service.ExRescueQueryExportService;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.check.service.read.CheckReadService;
import com.mcxx.modules.miniSalvation.family.dto.*;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.entity.OrganizationEntity;
import com.mcxx.modules.miniSalvation.family.param.*;
import com.mcxx.modules.miniSalvation.family.service.read.*;
import com.mcxx.modules.miniSalvation.order.dto.OrderApproveDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderInHouseDTO;
import com.mcxx.modules.miniSalvation.order.service.read.InHouseReadService;
import com.mcxx.modules.miniSalvation.order.service.read.NoticeBoardDetailReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderDetailReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryChangeReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.MemberSalaryChangeReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Validated
@Api("特困查询管理控制类")
@RestService({"extremePoverty/query/v2"})
public class ExSalvationQueryController extends DefaultRestService {

    private static final Logger logger = LoggerFactory.getLogger(ExSalvationRecheckController.class);

    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private InHouseReadService inHouseReadService;
    @Autowired
    private ExBacklogService exBacklogService;
    @Autowired
    private FamilySalaryReadService familySalaryReadService;
    @Autowired
    private AppraisalReadService appraisalReadService;
    @Autowired
    private SpecialPeopleReadService specialPeopleReadService;
    @Autowired
    FamilySalaryChangeReadService salaryChangeReadService;
    @Autowired
    private MemberSalaryChangeReadService memberSalaryChangeReadService;
    @Autowired
    private ExRescueQueryExportService exRescueQueryExportService;
    @Autowired
    private ExQueryService exQueryService;
    @Autowired
    private OrderDetailReadService orderDetailReadService;
    @Autowired
    private NoticeBoardDetailReadService noticeBoardDetailReadService;
    @Autowired
    private OrganizationReadService organizationReadService;
    @Autowired
    private CheckReadService checkReadService;
    @Autowired
    private FamilyMainReadService familyMainReadService;

    
    @PostMapping("getOldAgeHomes")
    @ApiOperation("根据地区读取相应的机构")
    public void getOldAgeHomes(){
        logger.info("根据地区读取相应的机构");
        String areaCode = getCurUserAccount().getAreaCode().substring(0,6);
        List<OrganizationEntity> orgList = organizationReadService.getOldAgeHomesByAreaCode(areaCode);
        setData(orgList);
        logger.info("根据地区读取相应的机构,结果是：{}", orgList);
    }
    
    @PostMapping("getSupplyFamilyList")
    @ApiOperation("查询供养家庭列表")
    public void getSupplyFamilyList(SalvationFamilyParam param, PageParam pageParam) {
        logger.info("查询供养家庭列表列表，参数是：{},{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<SalvationFamilyDTO> page = orderReadService.getSupplyFamilyList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查询供养家庭列表结束，参数是：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("getSupplyObjectList")
    @ApiOperation("查询供养对象列表")
    public void getSupplyObjectList(SalvationObjectParam param, PageParam pageParam) {
        logger.info("查询供养对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<SalvationObjectDTO> page = orderReadService.getSupplyObjectList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查询供养对象列表结束，参数是：{}", JSON.toJSONString(page));
    }

    @ApiOperation("查询核对信息")
    @PostMapping("getCheckList")
    public void getCheckList(@RequestParam("orderId")String orderId){
        logger.info("查询核对列表信息，参数：{}", orderId);
        List<CheckEntity> entities = checkReadService.selectCheckList(orderId);
        setData(entities);
        logger.info("查询核对列表信息，参数：{}", JSON.toJSONString(entities));
    }

    
    @GetMapping("getFamilyCountSummary")
    @ApiOperation("根据条件统计供养家庭总户数、总人数、总金额")
    public void getFamilyCountSummary(SalvationFamilyParam param) {
        logger.info("开始统计总户数、总人数、总金额，参数：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Map data = orderReadService.getFamilyCountSummary(param,accountVo);
        setData(data);
        logger.info("根据条件统计供养家庭总户数、总人数、总金额完成，参数：{}", JSON.toJSONString(data));
    }

    
    @GetMapping("getObjectCountSummary")
    @ApiOperation("根据条件统计供养对象总人数、总金额")
    public void getObjectCountSummary(SalvationObjectParam param) {
        logger.info("根据条件统计保障对象总人数、总金额，参数：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Map data = orderReadService.getObjectCountSummary(param,accountVo);
        setData(data);
        logger.info("根据条件统计供养对象总人数、总金额完成，结果：{}", JSON.toJSONString(data));
    }

    
    @PostMapping("getProvertyObjectList")
    @ApiOperation("查看扶贫办对象列表")
    public void getProvertyObjectList(ProvertyObjectParam param, PageParam pageParam) {
        logger.info("开始查询保障对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<ProvertyObjectDTO> page = orderReadService.getExtProvertyObjectList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查看扶贫办对象列表，参数是：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("getSalvationFamilyChangeList")
    @ApiOperation("查询家庭动态列表")
    public void getSalvationFamilyChangeList(SalvationFamilyChangeParam param, PageParam pageParam) {
        logger.info(String.format("查询家庭动态列表，请求参数：{%s} 分页 :{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<SalvationFamilyChangeDTO> page = salaryChangeReadService.getSalvationFamilyList(param, pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info(String.format("查询家庭动态列表完成，结果：{}", JSON.toJSONString(page)));
    }

    
    @PostMapping("getSalvationObjectChangeList")
    @ApiOperation("查询对象动态列表")
    public void getSalvationObjectChangeList(SalvationFamilyChangeParam param, PageParam pageParam) {
        logger.info(String.format("查询对象动态列表，请求参数：{%s},分页：{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<SalvationObjectChangeDTO> page = memberSalaryChangeReadService.getSalvationObjectChangeList(param, pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info(String.format("查询对象动态列表完成，结果：{}", JSON.toJSONString(page)));
    }

    
    @PostMapping("getFamilySpecialList")
    @ApiOperation("查询特殊人员备案列表")
    public void getFamilySpecialList(FamilySpecialParam param, PageParam pageParam) {
        logger.info(String.format("查询特殊人员备案列表，请求参数：{%s},分页：{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<FamilySpecialObjectDTO> page = specialPeopleReadService.listByCondition(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询特殊人员备案列表完成，结果：{}",JSON.toJSONString(page));
    }

    

    
    @GetMapping("getOrderDetailById")
    @ApiOperation("查询未办结的业务详情")
    public void getOrderDetailById(@V({"notnull"}) String orderId) {
        logger.info("查询未办结的业务详情，参数：{}", orderId);
        MininumLivingApplyDTO applyDto = exQueryService.getOrderDetail(orderId);
        setData(applyDto);
        logger.info("查询未办结的业务详情完成，结果：{}", JSON.toJSONString(applyDto));
    }

    
    @GetMapping("listFamilyMember")
    @ApiOperation("根据orderId查询状态正常的家庭成员列表")
    public void listFamilyMember(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询状态正常的家庭成员列表，参数：{}", orderId);
        List list = exQueryService.listFamilyMember(orderId);
        setData(list);
        logger.info("根据orderId查询状态正常的家庭成员列表完成，结果：{}", JSON.toJSONString(list));
    }

    
    @GetMapping("getMemberByIdAndOrderId")
    @ApiOperation("根据id查询指定家庭成员信息")
    public void getMemberByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        logger.info("根据id和orderId查询指定家庭成员信息，参数：{}", id);
        FamilyMemberEntity memberEntity = exQueryService.getFamilyMember(id, orderId);
        setData(memberEntity);
        logger.info("根据id和orderId查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(memberEntity));
    }
    
    @GetMapping("getMemberBankByIdAndOrderId")
    @ApiOperation("根据id和orderId查询指定家庭成员信息")
    public void getMemberBankByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        logger.info("根据id和orderId查询指定家庭成员信息，参数：{}", id);
        FamilyMemberDTO familyMemberDTO = exQueryService.getFamilyMemberBankInfo(id, orderId,Constant.BusinessCode.TEKUN);
        setData(familyMemberDTO);
        logger.info("根据id和orderId查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(familyMemberDTO));
    }
    
    @GetMapping("listHouseholdSurvey")
    @ApiOperation("根据orderId查询入户调查记录列表")
    public void listHouseholdSurveys(@V({"notnull"}) String orderId, @V({"notnull"}) String userAreaLevel) {
        logger.info("查询入户调查记录列表，参数为{}", orderId);
        List<OrderInHouseDTO> list = inHouseReadService.selectListByOrderId(orderId, userAreaLevel);
        setData(list);
        logger.info("查询入户调查记录列表完成，结果为{}", JSON.toJSONString(JSON.toJSONString(list)));
    }

    
    @GetMapping("getInHouseRecord")
    @ApiOperation("根据orderId查询入户调查记录（乡镇/区县）")
    public void getInHouseRecord(@V("notnull") String inHouseId){
        logger.info("根据orderId查询入户调查记录，参数是：{}", inHouseId);
        OrderInHouseDTO orderInHouseDTO = inHouseReadService.queryNewById(inHouseId);
        setData(orderInHouseDTO);
        logger.info("根据orderId查询入户调查记录，结果是：{}", orderInHouseDTO);
    }

    
    @GetMapping("listDemocracyAppraisal")
    @ApiOperation("根据orderId查询民主评议信息")
    public void listDemocracyAppraisal(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询民主评议信息，参数：{}", orderId);
        List<DemocraticAppraisalDTO> dto = appraisalReadService.getAppraisalByOrderId(orderId);
        setData(dto);
        logger.info("根据orderId查询民主评议信息完成，结果：{}", dto);
    }
    
    @GetMapping("getDemocracyAppraisal")
    @ApiOperation("根据Id查询民主评议信息")
    public void getDemocracyAppraisal(@V({"notnull"}) String appraisalId) {
        logger.info("根据Id查询民主评议信息，参数：{}", appraisalId);
        DemocraticAppraisalDTO dto = appraisalReadService.getAppraisaById(appraisalId);
        setData(dto);
        logger.info("根据Id查询民主评议信息，结果：{}", dto);
    }

    
    @GetMapping("getSalvationAudit")
    @ApiOperation("查询审核信息")
    public void getSalvationAudit(@V({"notnull"}) String orderId) {
        logger.info("查询审核信息，参数为{}", orderId);
        List<OrderApproveDTO>  result = orderDetailReadService.getApproveList(orderId, Constant.CheckDatailStatus.he,null);
        setData(result);
        logger.info("查询审核信息完成，结果为{}", JSON.toJSONString(result));
    }

    
    @GetMapping("getFamilySalaryInfo")
    @ApiOperation("查询家庭救助待遇")
    public void getFamilySalaryInfo(@V({"notnull"}) String orderId){
        logger.info("查询家庭救助待遇，参数为{}", orderId);
        Map<String, Object> result = familySalaryReadService.getFamilySalaryInfo(orderId,null,null);
        setData(result);
        logger.info("查询家庭救助待遇，结果为{}", JSON.toJSONString(result));
    }

    
    @GetMapping("getSalvationApprove")
    @ApiOperation("查询审批信息")
    public void getSalvationApprove(@V({"notnull"}) String orderId){
        logger.info("查询审核信息，参数：{}", orderId);
        BaseResult<OrderApproveDTO>  result = exBacklogService.getSalvationApprove(orderId);
        setData(result.getData());
        logger.info("查询审核信息完成，结果：{}", JSON.toJSONString(result.getData()));
    }

    
    @GetMapping("getSalvationAudits")
    @ApiOperation("查询审核信息")
    public void getSalvationAudits(@V({"notnull"}) String orderId) {
        logger.info("查询审核信息，参数为{}", orderId);
        List<OrderApproveDTO>  result = orderDetailReadService.getApproveList(orderId, Constant.CheckDatailStatus.Pi,null);
        setData(result);
        logger.info("查询审核信息完成，结果为{}", JSON.toJSONString(result));
    }

    
    @GetMapping("getSalvationRevokes")
    @ApiOperation("查询撤销信息")
    public void getSalvationRevokes(@V({"notnull"}) String orderId) {
        logger.info("查询撤销信息，参数为{}", orderId);
        List<OrderApproveDTO>  result = orderDetailReadService.getRevokeList(orderId, Constant.CheckDatailStatus.revoke);
        setData(result);
        logger.info("查询撤销信息完成，结果为{}", JSON.toJSONString(result));
    }

    
    @GetMapping("getMemberAssetsList")
    @ApiOperation("根据familyId获取员财产收入列表")
    public void getMemberAssetsList(@V({"notnull"}) String familyId, @V({"notnull"}) String orderId) {
        logger.info("根据familyId获取员财产收入列表，参数是：{}", familyId);
        Map<String, Object> list = exQueryService.getMemberAssets(familyId, orderId);
        setData(list);
        logger.info("根据familyId获取员财产收入列表，结果是：{}", list);
    }

    
    @PostMapping("getCalculateHtml")
    @ApiOperation("根据id获取员财产收入列表")
    public void getCalculateHtml(@V({"notnull"}) String familyId) {
        logger.info("根据familyId查询精准计算页面，参数是：{}", familyId);
        String htmlName = familyMainReadService.selectCalculateHtml(familyId);
        setData(htmlName);
        logger.info("根据familyId查询精准计算页面，结果是：{}", htmlName);
    }

    

    
    
    @PostMapping({"downLoadRescueList"})
    public void exportOrgOpLog(HttpServletResponse response, @RequestBody SalvationObjectParam param) throws IOException {
        ExcelInfo excelInfo = exRescueQueryExportService.exportExcel(param.getExportType(),param.getOrderIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }

    
    @PostMapping({"downLoadRescueFamilyList"})
    public void downLoadRescueFamilyList(HttpServletResponse response, @RequestBody SalvationFamilyParam param) throws IOException {
        ExcelInfo excelInfo = exRescueQueryExportService.exportExcelFamilySave(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    
    @PostMapping({"downLoadChangeList"})
    public void downLoadChangeList(HttpServletResponse response,@RequestBody SalvationFamilyChangeParam param) throws IOException {
        ExcelInfo excelInfo = exRescueQueryExportService.exportExcelChange(param.getExportType(),param.getOrderIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }

    

    
    @GetMapping("listNoticeDetail")
    @ApiOperation("根据orderId查询公示列表")
    public void listNoticeDetail(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询公示列表，参数：{}", orderId);
        List list = noticeBoardDetailReadService.queryNoticeByOrderId(orderId);
        setData(list);
        logger.info("根据orderId查询公示列表，结果：{}", JSON.toJSONString(list));
    }
}
